package com.zed.code.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zed.code.base.TResponse;
import com.zed.code.controller.V1PushController;
import com.zed.code.request.PushReqInfo;
import com.zed.code.request.QueryEquipChargeStatsReqForFD;
import com.zed.code.response.ChargeDetail;
import com.zed.code.response.NEquipChargeStatusParam;
import com.zed.code.response.QueryEquipChargeStatsResForFD;
import com.zed.code.service.PushService;
import com.zed.code.service.QueryService;
import com.zed.code.service.impl.RedisHandle;
import com.zed.code.util.GDEVConfig;
import com.zed.code.util.PubFetchTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component
public class OrderTask {

    @Autowired
    private RedisHandle redisHandle;
    @Autowired
    private QueryService queryService;
    @Autowired
    private PushService pushService;

    public final static String OPERATOR_ID = "314838869"; //组织机构代码(企业名称，营业执照)：

    private static Logger log = LoggerFactory.getLogger(OrderTask.class);

    //50秒推送一个订单消息
    @Scheduled(cron = "0/50 * * * * ? ") // 间隔50秒执行
    public void OrderChargeStatus() throws Exception {

        List list=redisHandle.getList("OrderChargeStatus");
        if (list!=null&&list.size()>0){
            List<PushReqInfo> push = new ArrayList<PushReqInfo>();
            log.debug("定时任务orderId={}",list.toString());
            Iterator<Integer> iterator=list.iterator();

            while (iterator.hasNext()){
                Integer orderId =iterator.next();
                QueryEquipChargeStatsReqForFD req = new QueryEquipChargeStatsReqForFD();
                req.setStartChargeSeq(orderId.toString());
                TResponse<QueryEquipChargeStatsResForFD> res = queryService.queryEquipChargeStatus(req);
                JSONObject didiResult = PubFetchTool.dealParam(JSON.toJSONString(equipChargeStatus(res.getData())), GDEVConfig.AES_KEY, GDEVConfig.AES_IV, GDEVConfig.SIGN_KEY);
                log.info("封装后的参数{}", JSON.toJSONString(didiResult));
                PushReqInfo didi = new PushReqInfo();
                didi.setData(didiResult.toJSONString());
                didi.setMethod("notification_equip_charge_status");
                didi.setOperatorSecret(GDEVConfig.OPERATOR_SECRET);
                didi.setURL(GDEVConfig.URL);
                didi.setAesKey(GDEVConfig.AES_KEY);
                didi.setAesIv(GDEVConfig.AES_IV);
                didi.setSignKey(GDEVConfig.SIGN_KEY);
                push.add(didi);
            }
            pushService.pushToPlatform(push);
        }

    }
    private NEquipChargeStatusParam equipChargeStatus(QueryEquipChargeStatsResForFD bodyInfo) throws Exception {
        NEquipChargeStatusParam nEquipChargeStatusParam = new NEquipChargeStatusParam();
        String orderId = bodyInfo.getStartChargeSeq();
        if (orderId.length()!=27){//平台自己的订单
            orderId=OPERATOR_ID+"000"+orderId;
        }
        if (bodyInfo.getStartChargeSeqStat()!=0){//不在充电
            redisHandle.removeListValue("OrderChargeStatus",orderId);
        }
        nEquipChargeStatusParam.setStartChargeSeq(orderId);
        nEquipChargeStatusParam.setStartChargeSeqStat(bodyInfo.getStartChargeSeqStat());
        nEquipChargeStatusParam.setConnectorID(bodyInfo.getConnectorID());
        nEquipChargeStatusParam.setConnectorStatus("3");
        nEquipChargeStatusParam.setCurrentA(bodyInfo.getCurrentA());
        nEquipChargeStatusParam.setCurrentB(bodyInfo.getCurrentB());
        nEquipChargeStatusParam.setCurrentC(bodyInfo.getCurrentC());
        nEquipChargeStatusParam.setVoltageA(bodyInfo.getVoltageA());
        nEquipChargeStatusParam.setVoltageB(bodyInfo.getVoltageB());
        nEquipChargeStatusParam.setVoltageC(bodyInfo.getVoltageC());
        nEquipChargeStatusParam.setSoc(bodyInfo.getSoc());
        nEquipChargeStatusParam.setStartTime(bodyInfo.getStartTime());
        nEquipChargeStatusParam.setChargeModel(bodyInfo.getStartChargeSeqStat());
        nEquipChargeStatusParam.setEndTime(bodyInfo.getEndTime());
        nEquipChargeStatusParam.setTotalPower(bodyInfo.getTotalPower());
        nEquipChargeStatusParam.setElecMoney(bodyInfo.getElecMoney());
        nEquipChargeStatusParam.setSeviceMoney(bodyInfo.getElecMoney());
        nEquipChargeStatusParam.setTotalMoney(bodyInfo.getTotalMoney());
        nEquipChargeStatusParam.setSumPeriod(bodyInfo.getSumPreiod());
        ChargeDetail chargeDetail = new ChargeDetail();
        chargeDetail.setDetailElecMoney(bodyInfo.getElecMoney());
        chargeDetail.setDetailEndTime(bodyInfo.getEndTime());
        chargeDetail.setDetailPower(bodyInfo.getTotalPower());
        chargeDetail.setDetailSeviceMoney(bodyInfo.getServiceMoney());
        chargeDetail.setDetailStartTime(bodyInfo.getStartTime());
        chargeDetail.setElecPrice(0);
        chargeDetail.setServicePrice(0);
        List<ChargeDetail> chargeDetailList=new ArrayList<>();
        chargeDetailList.add(chargeDetail);
        nEquipChargeStatusParam.setChargeDetails(chargeDetailList);
        return nEquipChargeStatusParam;
    }
}
